const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//活动id
    quiz_id:"",//提问id
    banner:"",//banner
    questionsList:"",//提问数据
    page:1,
    size:6,
    is_verify:0,
    footer_more:"",
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0,//判断是否切换tab  0是切换，重新拿数据  1是上拉，追加数据
    is_snap:"",//同问点赞 1没有点赞 0有点赞
    index:"",//同问点赞
    isshow:false, //弹窗是否显示
    posterData:"", //海报数据
    count:"",//待审核数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBannerMesData();
    this.getQuestionsListMesData();
    this.getShareEwmMesData();//分享海报
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  clickNav: function (e) {
    this.setData({
      is_verify: e.currentTarget.dataset.id,
      page: 1,
      is_clickTab:0

    })
    this.getQuestionsListMesData();
  },
  //获取图片
  getBannerMesModel:new BaseModel({
    url: "detailQuiz",
    needAjaxLoading:true,
    loadingText:"玩命加载中",
    method:"get"
  }),
  getBannerMesData(){
    this.getBannerMesModel
        .fetch({
          activity_id:this.data.id
        })
        .then(res => {
          this.setData({
            banner:res.data.background_img
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  //获取问题列表
  getQuestionsListMesModel:new BaseModel({
    url: "employees/listQuiz",
    needAjaxLoading:true,
    loadingText:"玩命加载中",
    method:"get"
  }),
  getQuestionsListMesData(){
    this.getQuestionsListMesModel
        .fetch({
          activity_id:this.data.id,
          page:this.data.page,
          size:this.data.size,
          is_verify:this.data.is_verify,
        })
        .then(res => {
          this.setData({
              count:res.count
          })
          if(this.data.is_clickTab==0){
            this.setData({
              questionsList: res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              var questionsList_list = this.data.questionsList;
              for(var i=0;i<res.data.length;i++){
                questionsList_list.push(res.data[i])
              }
              this.setData({
                questionsList: questionsList_list,
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*预览图片*/
  previewImage:function(e){
    //获取当前图片的下标
    var imgArr = e.currentTarget.dataset.imagelist;
    var currentImg = imgArr[e.target.dataset.img];
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: imgArr
    })
  },
  /*待审核*/
  examine:function(e){

    var that = this;
    that.setData({
      quiz_id:e.currentTarget.dataset.id,
      index:e.currentTarget.dataset.index,
    })
    wx.showModal({
      title: '确认审核通过吗？',
      content: '审核',
      success(res) {
        if (res.confirm) {
          that.getExamineMesData(); //审核
          var message = that.data.questionsList;
          for (var i=0;i<message.length;i++) { //遍历列表数据
            if(that.data.index==i){
              message.splice(that.data.index,1);
            }
          }
          that.setData({
            questionsList: message,
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //审核
  getExamineMesModel:new BaseModel({
    url: "employees/verifyQuiz",
    needAjaxLoading:true,
    loadingText:"玩命加载中",
    method:"get"
  }),
  getExamineMesData(){
    this.getExamineMesModel
        .fetch({
          quiz_id:this.data.quiz_id
        })
        .then(res => {

        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*分享海报*/
  sharePoster:function(){
    this.setData({
      isshow:true
    })
  },
  /*保存图片*/
  saveImage:function(){
    wx.downloadFile({
      url: this.data.posterData,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权")
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          },
          complete(res){
            console.log(res);
          }
        })
      }
    })

  },
  /*点击隐藏海报*/
  share_posterHide:function(){
    this.setData({
      isshow:false,
    })
  },
  //分享二维码
  getShareEwmMesModel:new BaseModel({
    url: "employees/shareQuiz",
    needAjaxLoading:true,
    loadingText:"玩命加载中",
    method:"get"
  }),
  getShareEwmMesData(){
    this.getShareEwmMesModel
        .fetch({
          activity_id:this.data.id
        })
        .then(res => {
          this.setData({
            posterData:res.data
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*禁止页面滚动*/
  preventTouchMove:function(e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getBannerMesData();
    this.getQuestionsListMesData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getBannerMesData();
    this.getQuestionsListMesData();
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1  //上拉加载标识
    })
    this.getQuestionsListMesData();
  },

  /**
   * 用户点击右上角分享
   */
 /* onShareAppMessage: function () {

  }*/
})